﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Entity;
using BLL;

namespace Web.admin.OutterManage.OutterAwardsManage
{
    public partial class awardStu : System.Web.UI.Page
    {
        StudentService studentService = new StudentService();
        ParticipantService participantService = new ParticipantService();
        GroupsService groupsService = new GroupsService();
        ContestService contestService = new ContestService();
        ClassService classService = new ClassService();
        AcademyService academyService = new AcademyService();
        CompetitionService competitionService = new CompetitionService();
        AwardService awardService = new AwardService();
        protected string OPERATION = "test";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AwarListBind("", AwardsList);
                //根据传递过来的参数判断操作类型
                string operation = Request["OPERATION"];
                if (operation == null)
                {
                    string contestId = Request["ContestId"];
                    if (String.IsNullOrEmpty(contestId))
                        return;
                    AddInner(contestId);
                    GroupListBind("无", GroupsList, contestId);
                    ConId.Value = contestId;
                }
                else
                {
                    OPERATION = operation;
                    string compId = Request["id"];
                    if (compId == null)
                        return;
                    if (operation == "Modify")
                    {
                        ModifyInner(compId);
                    }
                    else if (operation == "Delete")
                    {
                        DeleteInner(compId);
                    }
                }
            }
        }
        /// <summary>
        /// 获奖等级绑定
        /// </summary>
        /// <param name="firstText">第一项文本</param>
        /// <param name="ddl">dropdownlist控件</param>
        protected void AwarListBind(string firstText, DropDownList ddl)
        {
            ddl.Items.Clear();
            if (!string.IsNullOrEmpty(firstText))
            {
                ddl.Items.Add(new ListItem(firstText, ""));
            }
            IList<BoAwards> boawardList = awardService.GetAllAwards();
            foreach (BoAwards boaward in boawardList)
            {
                ddl.Items.Add(new ListItem(boaward.AWA_NAME, boaward.AWA_ID));
            }
        }
        /// <summary>
        /// 小组列表的绑定
        /// </summary>
        /// <param name="firstText">首项文本</param>
        /// <param name="ddl">DropDownList控件</param>
        /// <param name="ConId">竞赛id</param>
        protected void GroupListBind(string firstText, DropDownList ddl, string ConId)
        {
            BoContests contestmodel = new BoContests() { CON_ID = ConId };
            IList<BoGroups> groupList = participantService.GetContestGroups(contestmodel);
            ddl.Items.Clear();
            if (!string.IsNullOrEmpty(firstText))
            {
                ddl.Items.Add(new ListItem(firstText, ""));
            }
            foreach (BoGroups BoGroup in groupList)
            {
                ddl.Items.Add(new ListItem(BoGroup.GRO_NAME, BoGroup.GRO_ID));
            }
        }
        /// <summary>
        /// 根据竞赛id来绑定相应的竞赛
        /// </summary>
        /// <param name="contestId"></param>
        protected void AddInner(string contestId)
        {
            BoContests contest = contestService.getContest(contestId);
            List<BoContests> contests = new List<BoContests>();
            contests.Add(contest);

            ContestsList.DataSource = contests;
            ContestsList.DataTextField = "CON_NAME";
            ContestsList.DataValueField = "CON_ID";
            ContestsList.DataBind();
        }

        /// <summary>
        /// 根据参赛id来修改相应的参赛数据
        /// </summary>
        /// <param name="parId">参赛id</param>
        protected void ModifyInner(string parId)
        {
            //2013年6月5日修改ajax时注释的内容
            BoParticipants par = participantService.getParticipant(parId);
            ParId.Value = par.PAR_ID;
            stuCode_txt.Value = par.STU_CODE;
            //TBcode.Enabled = false;
            stuCode_txt.Attributes["Readonly"] = "readonly";
            //studentInfo.Text = par.STU_NAME;

            AddInner(par.PAR_CONTESTS);

            BoGroups group = groupsService.getGroups(par.PAR_GROUPS);
            List<BoGroups> groups = new List<BoGroups>();
            groups.Add(group);
            GroupsList.DataSource = groups;
            GroupsList.DataTextField = "GRO_NAME";
            GroupsList.DataValueField = "GRO_ID";
            GroupsList.DataBind();
            GroupsList.SelectedValue = par.PAR_GROUPS;

            if (par.PAR_AWARDS != "" && par.PAR_AWARDS != "-1")
                AwardsList.SelectedValue = par.PAR_AWARDS;

            foreach (ListItem item in IsSpecialtyList.Items)
            {
                if (par.PAR_SPECIALTY.ToString() == item.Value)
                    item.Selected = true;
                else
                    item.Selected = false;
            }
        }
        /// <summary>
        /// 根据id删除相应的学生
        /// </summary>
        /// <param name="parId">参赛id</param>
        protected void DeleteInner(string parId)
        {
            BoParticipants par = participantService.getParticipant(parId);
            par.PAR_AWARDS = "-1";
            participantService.UpdateParticipant(par);

            Response.Redirect("OutterAwards.aspx?id=" + par.PAR_CONTESTS);

        }
        /// <summary>
        /// 保存按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (ParId.Value == "")
            {
                return;
            }
            BoParticipants par = participantService.getParticipant(ParId.Value);
            par.PAR_AWARDS = AwardsList.SelectedValue;
            if (GroupsList.SelectedValue != "")
                par.PAR_GROUPS = GroupsList.SelectedValue;
            participantService.UpdateParticipant(par);
            Response.Redirect("OutterAwards.aspx?id=" + par.PAR_CONTESTS);

        }
    }
}